package com.qianchang.microfinance.view.mine;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.ClearEditText;
import com.dm.library.widgets.indexlib.IndexBar.widget.DividerItemDecoration;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.MyBillBean;
import com.qianchang.microfinance.bean.OverListBean;
import com.qianchang.microfinance.bean.RechargeFixBean;
import com.qianchang.microfinance.bean.RechargeListItemBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.utils.MoneyTextWatcher;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.base.EmptyLayout;
import com.qianchang.microfinance.view.base.RecyclerRefreshLayout;
import com.qianchang.microfinance.view.dialog.alert.RechargeDialog;
import com.qianchang.microfinance.view.hsrecharge.HSReChargeActivity;
import com.qianchang.microfinance.view.mine.bill.PayBackActivity;
import com.qianchang.microfinance.view.mine.bill.PrePayBackActivity;
import com.qianchang.microfinance.view.mine.bill.SettlementActivity;
import com.qianchang.microfinance.widgets.RecyclerViewDivider;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

import static com.qianchang.microfinance.view.base.EmptyLayout.NO_DATA;


/**
 *  2018.7.11 cgl
 */


public class RechargeFixActivity  extends BaseActivity implements View.OnClickListener {
    //逾期金额
    //获取个人还款计划中即将还款的金额

    @BindView(R.id.rv_info_id)
    RecyclerView recyclerViewInfo;
    @BindView(R.id.error_layout)
    EmptyLayout errorLayout;
    @BindView(R.id.refreshLayout)
    RecyclerRefreshLayout refreshLayout;
    @BindView(R.id.et_input_money)
    TextView et_input_money;
    @BindView(R.id.tv_rechage)
    TextView tv_rechage;
    private RecyclerViewDivider recyclerViewDivider;
    private LinearLayoutManager linearLayoutManager;
    private RechargeFixRvAdapter adapter;
    private List<RechargeListItemBean> list;
    public static final String ENTITY_KEY = "MyBillBean_Bean";
    private double re_money;
    private   double dd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rechargefix);
        ButterKnife.bind(this);
        setTitleName(getString(R.string.recharge));
        initView();
        initData();
    }


    @Override
    protected void initView() {
        super.initView();
        initRecyclerView();
        tv_rechage.setOnClickListener(this);
        errorLayout.findViewById(R.id.animProgress).setVisibility(View.GONE);
        et_input_money.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG ); //下划线
        et_input_money.getPaint().setAntiAlias(true);//抗锯齿
        errorLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDetail();
            }
        });
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getDetail();
            }
        });
        et_input_money.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RechargeDialog rechargeDialog = RechargeDialog.newInstance("请输入充值金额");
                rechargeDialog.setOnDialogClickListener(new RechargeDialog.OnDialogClickListener() {
                    @Override
                    public void agree(String input) {
                        if(input!=null&&!input.equals("")&&!input.equals("0")){
                            et_input_money.setText(input);
                            String str = et_input_money.getText().toString().trim();
                             dd =Double.parseDouble(input);
                            re_money=Double.parseDouble(et_input_money.getText().toString().trim());
                            rechargeDialog.dismiss();
                        }
                       else  if(input.equals("")||re_money<=0){
                            ToastUtil.getInstant().show(RechargeFixActivity.this,"请输入大于0的金额");
                        }

                    }

                    @Override
                    public void cancel() {

                    }
                });
                rechargeDialog.show(getSupportFragmentManager(), getClass().getSimpleName());
            }
        });
    }

    private void initRecyclerView(){
        linearLayoutManager = new LinearLayoutManager(RechargeFixActivity.this);
        recyclerViewInfo.setLayoutManager(linearLayoutManager);
        adapter = new RechargeFixRvAdapter(RechargeFixActivity.this);
        recyclerViewInfo.setAdapter(adapter);
        recyclerViewDivider = new RecyclerViewDivider(RechargeFixActivity.this, DividerItemDecoration.VERTICAL_LIST);
        recyclerViewInfo.addItemDecoration(recyclerViewDivider);
        recyclerViewInfo.setHasFixedSize(true);
        recyclerViewInfo.setNestedScrollingEnabled(false);
        adapter.setSettleButtonInterface(new FixSettleButtonInterface());                       //设置提前结算监听
        adapter.setRepayMentButtonInterface(new FixRepayMentButtonInterface());                  //设置逾期还款监听
        adapter.setRechageButtonInterface(new FixRechageButtonInterface());                    //设置立即充值监听

    }


    @Override
    protected void initData() {
        super.initData();
        getDetail();
    }

    /**
     * 获取还款金额详情
     */
    private void getDetail() {
        show();
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.GET_BILL_RECHAR, params, new RequestCallBack<RechargeFixBean<RechargeListItemBean>>() {


            @Override
            public void onSuccess(JsonResult<RechargeFixBean<RechargeListItemBean>> response) {
                dismiss();
                refreshLayout.setRefreshing(false);
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    list = response.getObject().getList();
                    setAdapterData(list);
                } else {
                    ToastUtil.getInstant().show(RechargeFixActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                refreshLayout.setRefreshing(false);
                ToastUtil.getInstant().show(RechargeFixActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
                ToastUtil.getInstant().show(RechargeFixActivity.this, msg);
            }
        });
    }

    private void setAdapterData(List<RechargeListItemBean> list){

        if (list != null && list.size() > 0) {
            errorLayout.setVisibility(View.GONE);
            recyclerViewInfo.setVisibility(View.VISIBLE);
            adapter.setData(list);
        }else {
            errorLayout.setErrorType(NO_DATA);
            errorLayout.setVisibility(View.VISIBLE);
            recyclerViewInfo.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case  R.id.tv_rechage:
                if(re_money>0){
                    Bundle bundle = new Bundle();
                    bundle.putString("money",et_input_money.getText().toString().trim());
                    bundle.putString("rechargeType","3");
                    startActivity(HSReChargeActivity.class,bundle,true);

                }else {
                    ToastUtil.getInstant().show(RechargeFixActivity.this,"请输入充值金额");

                }

                break;

        }
    }

//

    //提前结算监听
    private class FixSettleButtonInterface implements RechargeFixRvAdapter.SettleButtonInterface {

        @Override
        public void onclick(View view, int position) {
            String projectId = "";
            RechargeListItemBean rechargeListItemBean = list.get(position);
            List<OverListBean> overList = list.get(position).getOverlist();
            if(overList != null && overList.size() > 0){
                projectId = overList.get(position).getProjectId();
            }
            if(rechargeListItemBean != null){
                projectId = rechargeListItemBean.getProjectId();
            }
            Bundle bundle = new Bundle();
            bundle.putString(ConstantAppInfo.PROJECT_ID, projectId);
            startActivity(PrePayBackActivity.class, bundle);                //跳转提前还款
        }
    }

    //逾期还款监听
    private class FixRepayMentButtonInterface implements RechargeFixRvAdapter.RepayMentButtonInterface {

        @Override
        public void onclick(View view, int position) {
            MyBillBean.Bean entity = new MyBillBean.Bean();
            List<OverListBean> overList = list.get(position).getOverlist();
            if(overList != null && overList.size() > 0){
                String projectId = overList.get(0).getProjectId();
                String overDays = overList.get(0).getOverdueDays();
                String period = overList.get(0).getPeriod();

                float interest = overList.get(0).getInterest();                 //利息
                float principal = overList.get(0).getPrincipal();               //本金
                float overdue = overList.get(0).getOverdue();               ////逾期费用
                float count = interest + principal + overdue;

                double mcount = Double.valueOf(String.valueOf(count));

                entity.setPaymentAmount(mcount);                                 //还款总额
                entity.setOverdueDays(Integer.valueOf(overDays));                    //逾期天数
                entity.setProjectId(projectId);                             //项目id
                entity.setPeriod(period);                                   //产品的哪一期
            }
            Bundle bundle = new Bundle();
            bundle.putSerializable(ENTITY_KEY, entity);                     // MyBillBean.Bean
            startActivity(PayBackActivity.class, bundle);                       //立即还款/逾期还款

        }
    }

    //立即充值监听
    private class FixRechageButtonInterface implements RechargeFixRvAdapter.RechageButtonInterface {

        @Override
        public void onclick(View view, int position) {
            RechargeListItemBean rechargeListItemBean = list.get(position);
            //去充值
            Bundle bundle = new Bundle();
            bundle.putString("money",rechargeListItemBean.getNow() + "");
            bundle.putString("rechargeType","3");
            startActivity(HSReChargeActivity.class,bundle,true);

        }
    }


}
